<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>

<head>
	<title>用户管理</title>
	<style type="text/css">
	span{
		display:inline-block;
	}
	
	span label{
		display:inline-block;
	}
	.controls label{
		display:inline-block;
	}
	</style>
	<script>
	
	var zNodes;
	var setting = {
		check: {
			enable: true,
			chkboxType: { "Y": "ps", "N": "ps" }
		},
	     data: {
	         simpleData: {
	             enable: true
	         }
	     }
	};
	var actionId = '${actionId}';
	if(actionId=="1"){
		//代理商--创建代理商用户
		url="${ctx}/user/tree/createUserTree";
	}else if(actionId=="2"){
		//企业--创建企业用户
		url="${ctx}/user/tree/updateUserTree/${user.id}";
	}
		$(document).ready(function() {
			//加载树
			 $.ajax({
		          	type: "POST",    
		          	url: url,                   
		          	dataType: "text",
		          	global: false,
		          	async: false,                      
		           	success: function (data) 
		           	{   
		          		zNodes=eval(data);
		          	},          
		          	error: function () {
		          		alert("Ajax请求数据失败!");
		        	}
			});
			$.fn.zTree.init($("#tree"), setting, zNodes);
			//getFrameHeight();
			//动态加载通道
			//彩信通道异步加载
			//验证必选字段
			$("#loginName").focus();
			$("#inputForm").validate({
				rules: {
   					loginName: {
						required:true,
						rangelength:[1,30],
						remote:{ 
　　		 					type: "post",
                            dataType: "json",    
　　							url:"${ctx}/user/user/checkLoginName?oldLoginName="+encodeURIComponent('${user.loginName}'),
　　	 						data:{
　　								loginName:function(){
									return $("#loginName").val();
　　								}
							}
						}                             
					},
					password:{
						required:true,
						rangelength:[3,50]
					},
					passwordConfirm:{
						required:true,
						equalTo:"#password"
					}
  				},
        		messages: {
   					loginName: {
						required:"登录名不能为空！",
						rangelength:jQuery.format("登录名位数必须在{0}到{1}字符之间！"),
						remote:jQuery.format("登录名已经被使用")
					},
					password:{
						required:"密码不能为空！",
						rangelength:jQuery.format("密码位数必须在{0}到{1}字符之间！")
					},
					passwordConfirm:{
						required:"确认密码不能为空！",
						equalTo:"确认密码与密码不一致！"
					}
  				}
			});
			$("#submitBtn").click(function(){
					getNodes();
					var value = $("#authCodeString").val();
			    	if($("#inputForm").valid()){
			    		if(value=="" || value==null){
							alert("权限不能为空");
							return false;
						}
						if(actionId=="1"){
							document.inputForm.action = "${ctx}/user/user/save";
						}else if(actionId=="2"){
							document.inputForm.action = "${ctx}/user/user/update";
						}
			     		$("#inputForm").submit();
			 		}
			});
			getFrameHeight();
		});
		function getNodes(){
			var treeObj = $.fn.zTree.getZTreeObj("tree");
			var nodes = treeObj.getCheckedNodes(true);
			var authCodeString = "";
			for(var i=0;i<nodes.length;i++){
				authCodeString = authCodeString + nodes[i].id;
				if(nodes.length-i>1){
					authCodeString = authCodeString+",";
				}
			}
			$("#authCodeString").attr("value",authCodeString);
		}
		function back(){
			window.location = "${ctx}/user/user";
		}
	</script>
</head>

<body>
       	<div>
               <div id="jCrumbs" class="breadCrumb module">
 					<ul>
				       <li>
				           <a href="#"><i class="icon-home" style="margin-top: 2px;"></i></a>
				       </li>
				       <li>
				            	用户管理
				       </li>
		    </ul>
		</div>					                    
	<div class="row-fluid">
 			<div class="span12">
 			<div style="float:right">
				<button class="btn" type="button"  onclick="back()">返 回</button>
			</div>
      		<h3 class="heading">用户信息</h3>
			<div class="row-fluid">
				<div class="span8">
						<form:form id="inputForm" name="inputForm" modelAttribute="user" cssClass="form-horizontal"
								action="" method="post">
							<input type="hidden" name="authCodeString" id="authCodeString" value=""/>
							<input type="hidden" name="id" id="id" value="${user.id}"/>	
							<fieldset>
								<div class="control-group formSep">
									<label for="u_fname" class="control-label"><font color="red">*</font>用户名:</label>
                					<div class="controls">
										<input type="text" id="loginName" name="loginName" class="input-xlarge" value="${user.loginName}" />
									</div>
								</div>
							 	<%-- <div class="control-group formSep">
									<label for="fileinput" class="control-label"><font color="red">*</font>用户名:</label>
									<div class="controls">
										<input type="text" id="userName" name="userName" class="input-xlarge" value="${user.userName}" />
									</div>
								</div>  --%>
								<c:if test="${actionId=='1' }">
								<div class="control-group formSep">
									<label for="fileinput" class="control-label"><font color="red">*</font>密码:</label>
									<div class="controls">
										<input type="text" id="password" name="password" class="input-xlarge" value="${user.password}" />
									</div>
								</div>
								<div class="control-group formSep">
									<label for="fileinput" class="control-label"><font color="red">*</font>确认密码:</label>
									<div class="controls">
										<input type="text" id="passwordConfirm" name="passwordConfirm" class="input-xlarge" value="${user.password}" />
									</div>
								</div>
								</c:if>
								 <div class="control-group formSep">
									<label for="fileinput" class="control-label">邮箱:</label>
									<div class="controls">
										<input type="text" id="email" name="email" class="input-xlarge" value="${user.email}" />
									</div>
								</div> 
								<div class="control-group formSep">
									<label for="fileinput" class="control-label">电话:</label>
									<div class="controls">
										<input type="text" id="mobile" name="mobile" class="input-xlarge" value="${user.mobile}" />
									</div>
								</div> 
								<div class="control-group formSep">
									<label for="fileinput" class="control-label">用户描述:</label>
									<div class="controls">
									<textarea id="descpt" name="descpt" cols="35" rows="4" class="numberStyle">${user.descpt}</textarea> 
									</div>
								</div>
								
								
								<div class="control-group formSep" id="AUTH_EDIT">
									<label for="u_fname" class="control-label"><font color="red">*</font>用户权限:</label>
									<div class="controls">
										<div class="zTreeDemoBackground left">
											<ul id="tree" class="ztree"></ul>
										</div>
									</div>
								</div>
								<div class="control-group">
									<div class="controls">
										<button class="btn btn-gebo" type="button" id="submitBtn">保 存</button>
										<button class="btn" type="reset">重 置</button>
									</div>
								</div>
							</fieldset>
						</form:form>
					</div>
				</div>
   			</div>
		</div>
     </div>
</body>
</html>
